import Vue from "vue";
import Router from "vue-router";
import module from "./module";
import cookie from "@utils/store/cookie";
import shareMsg from "../assets/js/common/shareConfig.js";
// import Index from "@views/home/Index";
// import Search from "@views/shop/GoodSearch";
// import Category from "@views/shop/GoodsClass";
import ShoppingCart from "@views/shop/ShoppingCart";
import GoodsList from "@views/shop/GoodsList";
// import GoodsCollection from "@views/shop/GoodsCollection";
import NotDefined from "@views/NotDefined";
import $store from "../store";
// import toLogin from "@libs/login";
import Loading from "@views/Loading";

Vue.use(Router);

const router = new Router({
  mode: "history",
  routes: [
    // {
    //   path: "/",
    //   name: "Index",
    //   meta: {
    //     title: "首页",
    //     keepAlive: true,
    //     footer: true,
    //     backgroundColor: "#fff"
    //   },
    //   component: Index
    // },
    // {
    //   path: "/customer/chat/:id/:productId?",
    //   name: "CustomerService",
    //   meta: {
    //     title: "客服聊天",
    //     keepAlive: false,
    //     auth: true
    //   },
    //   component: () => import("@views/user/CustomerService.vue")
    // },
    // {
    //   path: "/category/:pid?",
    //   name: "GoodsClass",
    //   meta: {
    //     title: "产品分类",
    //     keepAlive: true,
    //     footer: true,
    //     backgroundColor: "#fff"
    //   },
    //   component: Category
    // },
    {
      path: "/collection",
      name: "GoodsCollection",
      meta: {
        title: "心愿单",
        keepAlive: false,
        auth: true
      },
      component: () => import("@views/shop/GoodsCollection.vue")
    },
    {
      path: "/goodsManage/:id",
      name: "goodsManage",
      meta: {
        title: "商品橱窗",
        keepAlive: true,
        auth: true
      },
      component: () => import("@views/shop/goodsManage.vue")
    },
    // {
    //   path: "/search",
    //   name: "GoodSearch",
    //   meta: {
    //     title: "搜索商品",
    //     keepAlive: true,
    //     backgroundColor: "#fff"
    //   },
    //   component: Search
    // },
    // {
    //   path: "/news_detail/:id",
    //   name: "NewsDetail",
    //   meta: {
    //     title: "新闻详情",
    //     keepAlive: true,
    //     backgroundColor: "#fff"
    //   },
    //   component: () => import("@views/shop/news/NewsDetail.vue")
    // },
    // {
    //   path: "/news_list",
    //   name: "NewsList",
    //   meta: {
    //     title: "新闻",
    //     keepAlive: true,
    //     backgroundColor: "#fff"
    //   },
    //   component: () => import("@views/shop/news/NewsList.vue")
    // },
    // {
    //   path: "/evaluate_list/:id",
    //   name: "EvaluateList",
    //   meta: {
    //     title: "商品评分",
    //     keepAlive: true,
    //     auth: true
    //   },
    //   component: () => import("@views/shop/EvaluateList.vue")
    // },
    // {
    //   path: "/goods_evaluate/:id",
    //   name: "GoodsEvaluate",
    //   meta: {
    //     title: "商品评价",
    //     keepAlive: true,
    //     auth: true
    //   },
    //   component: () => import("@views/shop/GoodsEvaluate.vue")
    // },
    // {
    //   path: "/promotion",
    //   name: "GoodsPromotion",
    //   meta: {
    //     title: "促销单品",
    //     keepAlive: false
    //   },
    //   component: () => import("@views/shop/GoodsPromotion.vue")
    // },
    // {
    //   path: "/hot_new_goods/:type",
    //   name: "HotNewGoods",
    //   meta: {
    //     title: "热门榜单",
    //     keepAlive: false
    //   },
    //   component: () => import("@views/shop/HotNewGoods.vue")
    // },
    {
      path: "/detail/:id",
      name: "GoodsCon",
      meta: {
        title: "商品详情",
        keepAlive: false
      },
      component: () => import("@views/shop/GoodsCon.vue")
    },
    {
      path: "/SinglesDay",
      name: "SinglesPage",
      meta: {
        title: "双12活动",
        keepAlive: false
      },
      component: () => import("@views/shop/SinglesPage.vue")
    },
    {
      path: "/superSaleZJ",
      name: "superSaleZJ",
      meta: {
        title: "湛江广播年货节",
        keepAlive: false
      },
      component: () => import("@views/shop/superSaleZJ.vue")
    },
    {
      path: "/superSaleMould",
      name: "superSaleMould",
      meta: {
        title: "",
        keepAlive: false
      },
      component: () => import("@views/shop/superSaleMould.vue")
    },
    {
      path: "/superSaleZJdouble",
      name: "superSaleZJdouble",
      meta: {
        title: "湛江乡村振兴好物馆",
        keepAlive: false
      },
      component: () => import("@views/shop/superSaleZJdouble.vue")
    },
    {
      path: "/haikoushop",
      name: "haikoushop",
      meta: {
        title: "2021椰城少儿艺术盛典",
        keepAlive: false
      },
      component: () => import("@views/shop/haikouShop.vue")
    },
    {
      path: "/driverShop",
      name: "driverShop",
      meta: {
        title: "广元电商",
        keepAlive: false
      },
      component: () => import("@views/shop/driverShop.vue")
    },
    {
      path: "/superSale",
      name: "superSale",
      meta: {
        title: "听见年货节",
        keepAlive: false
      },
      component: () => import("@views/shop/superSale.vue")
    },
    {
      path: "/assembly",
      name: "assembly",
      meta: {
        title: "视听大会电商",
        keepAlive: false
      },
      component: () => import("@views/shop/assembly.vue")
    },
    // {
    //   path: "/clearStore",
    //   name: "clearStore",
    //   meta: {
    //     title: "开通店铺",
    //     keepAlive: false,
    //     auth: true
    //   },
    //   component: () => import("@views/clearStore/clearStore.vue")
    // },
    // {
    //   path: "/shop/storeList/:gonames?",
    //   name: "StoreList",
    //   meta: {
    //     title: "门店列表",
    //     keepAlive: false
    //   },
    //   component: () => import("@views/shop/StoreList.vue")
    // },
    {
      path: "/cart",
      name: "ShoppingCart",
      meta: {
        title: "购物车",
        keepAlive: false,
        footer: true,
        auth: true
      },
      component: ShoppingCart
    },
    {
      path: "/goods_list/:id",
      name: "GoodsList",
      meta: {
        title: "",
        keepAlive: false
      },
      component: GoodsList
    },
    {
      path: "/register",
      name: "Register",
      meta: {
        title: "注册",
        keepAlive: true
      },
      component: () =>
        import(/* webpackChunkName: "login" */ "@views/user/Register.vue")
    },
    // {
    //   path: "/change_password",
    //   name: "ChangePassword",
    //   meta: {
    //     title: "修改密码",
    //     keepAlive: true,
    //     backgroundColor: "#fff",
    //     auth: true
    //   },
    //   component: () =>
    //     import(/* webpackChunkName: "login" */ "@views/user/ChangePassword.vue")
    // },
    // {
    //   path: "/retrieve_password",
    //   name: "RetrievePassword",
    //   meta: {
    //     title: "找回密码",
    //     keepAlive: true
    //   },
    //   component: () =>
    //     import(
    //       /* webpackChunkName: "login" */ "@views/user/RetrievePassword.vue"
    //     )
    // },
    {
      path: "/login",
      name: "Login",
      meta: {
        title: "登录",
        keepAlive: true
      },
      component: () =>
        import(/* webpackChunkName: "login" */ "@views/user/Login.vue")
    },
    {
      path: "/radioFestival",
      name: "radioFestival",
      meta: {
        title: "2020年度电台盛典",
        keepAlive: false
      },
      component: () => import("@views/shop/radioFestival.vue")
    },
    {
      path: "/downLoad",
      name: "downLoad",
      meta: {
        title: "打开下载听见广播APP",
        keepAlive: false
      },
      component: () => import("@views/downLoad.vue")
    },
    ...module,
    {
      path: "/auth/:url",
      name: "Loading",
      meta: {
        title: " 加载中",
        keepAlive: true
      },
      component: Loading
    },
    {
      path: "*",
      name: "NotDefined",
      meta: {
        title: "页面找不到",
        keepAlive: true,
        home: false,
        backgroundColor: "#F4F6FB"
      },
      component: NotDefined
    }
  ],
  scrollBehavior(to, from) {
    from.meta.scrollTop = window.scrollY;
    return { x: 0, y: to.meta.scrollTop || 0 };
  }
});

const { back, replace } = router;

router.back = function() {
  this.isBack = true;
  back.call(router);
};
router.replace = function(...args) {
  this.isReplace = true;
  replace.call(router, ...args);
};

router.beforeEach((to, form, next) => {
  const { title, backgroundColor, footer, home, auth } = to.meta;
  console.log(to, form.name);
  $store.commit("SET_PAGE_NAME", form.name);
  // if (auth === true && !$store.state.app.token) {
  //   if (form.name === "Login") return;
  //   return toLogin(true, to.fullPath);
  // }
  if (to.name == "GoodsList" && form.name == "GoodsCon") {
    document.title = unescape(cookie.get("title"));
  } else if (to.name == "GoodsCon" && form.name == "GoodsList") {
    document.title = "商品详情";
  } else if (to.name == "GoodsCollection" && form.name == "GoodsCon") {
    document.title = "心愿单";
    if (cookie.get("from1") == "app") {
      shareMsg.shareConfig(
        "app",
        "0",
        "0",
        "res.data.storeInfo.store_name",
        "res.data.merchant_name",
        "shareUrl",
        "",
        "",
        "0"
      );
    }
  } else if (to.name == "ShoppingCart" && form.name == "GoodsCon") {
    document.title = "购物车";
    if (cookie.get("from1") == "app") {
      shareMsg.shareConfig(
        "app",
        "0",
        "0",
        "res.data.storeInfo.store_name",
        "res.data.merchant_name",
        "shareUrl",
        "",
        "",
        "0"
      );
    }
  } else {
    document.title = title || process.env.VUE_APP_NAME || "听见广播商城";
  }

  //判断是否显示底部导航
  footer === true ? $store.commit("SHOW_FOOTER") : $store.commit("HIDE_FOOTER");

  //控制悬浮按钮是否显示
  home === false ? $store.commit("HIDE_HOME") : $store.commit("SHOW_HOME");

  $store.commit("BACKGROUND_COLOR", backgroundColor || "#F5F5F5");

  if (auth) {
    $store.dispatch("USERINFO").then(() => {
      next();
    });
  } else next();
});

export default router;
